Use a USB HDD  to trigger a task to run a batch file
I am trying to create a scheduled task,so that when i connect my EXT HDD the batchfile will automaticly run and backup my data to my Win7 Pro PC Internal HDD. I have created the batchfile, and it will backup the ext hdd to the int hdd on logon. But how can i get it to run the batch file ONLY when i connect the ext HDD to the USB port. Need help, i have been pulling my hair out all day
October 27th, 2010 11:00am

You can use a 3rd party application like AutoIT, which will allow you to script a small file to watch for this change. The application can be found here -> http://www.autoitscript.com/autoit3/index.shtml And lucky for you, someone has already written the script needed to make this happen. It may need to be slightly edited to work for you, but I think its what your looking for. AutoIt v3 is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks in a way not possible or reliable with other languages (e.g. VBScript and SendKeys). AutoIt is also very small, self-contained and will run on all versions of Windows out-of-the-box with no annoying "runtimes" required! AutoIt was initially designed for PC "roll out" situations to reliably automate and configure thousands of PCs. Over time it has become a powerful language that supports complex expressions, user functions, loops and everything else that veteran scripters would expect. Here is the link to the thread -> http://www.autoitscript.com/forum/index.php?showtopic=42402 And here is the script Dim $DBT_DEVICEARRIVAL = "0x00008000" Dim $DBT_DEVICECOMPLETEREMOVAL = "0x00008004" Dim $USB_ATTENTION = "0x00000007" Dim $WM_DEVICECHANGE = 0x0219 Dim $Drives Dim $Drive_Type = "ALL" ; Set to ALL because some USB Drives are detected as Fixed Disks, and we don't want to miss those Dim $WATCH = False Dim $MyDrive = "STUFF" ;Get Initial List of Drives to Check Against UpdateDrives() ;Setup The GUI to watch for the DeviceChange Event GUICreate("") GUIRegisterMsg($WM_DEVICECHANGE, "DeviceChange") Func DeviceChange($hWndGUI, $MsgID, $WParam, $LParam) Switch $WParam Case $USB_ATTENTION ; This only happens when USB drives are inserted, so I use it to tell the difference between these and CDROMs $WATCH = True Case $DBT_DEVICECOMPLETEREMOVAL ; Whenever a Drive is Removed, Update the Drive List UpdateDrives() Case $DBT_DEVICEARRIVAL ; A drive was inserted ; Use $WATCH to tell if this was a CDROM or USB ; $WATCH = True, USB ; $WATCH = False, CDROM If $WATCH = True Then ; New USB Drive Was Detected, Time to Find it's Letter $New = FindNewDrive(); $New now has the Drive Letter of our New Drive, so USE IT!!! $Label = DriveGetLabel($New) If $Label == $MyDrive Then MsgBox(4096, "Info", "My Drive has been Inserted, Backup My Files!") EndIf ; Now Reset Drive List so more insertions can also be detected accurately UpdateDrives() EndIf EndSwitch EndFunc ;==>DeviceChange ; This just jumps through the new Drive List, comparing them until it finds the entry that is in the new one that isn't in the old one Func FindNewDrive() $Temp = DriveGetDrive( "REMOVABLE" ) For $i = 1 to $Temp[0] $Old = False For $j = 1 to $DRIVES[0] If $DRIVES[$j] == $Temp[$i] Then $Old = True Next If $Old == False Then Return $Temp[$i] Next EndFunc ;==>FindNewDrive ; Just to keep things neat, and so if Variables Ever Change, this makes updating easier Func UpdateDrives() $Drives = DriveGetDrive( $Drive_Type ) EndFunc ;==>UpdateDrives ; Main Loop to Keep the Program Open ; No Real Way of ending this program, except for just killing the process ; Which is what I want, an always on backup for my drive every time I insert it While 1 $GuiMsg = GUIGetMsg() ; This is needed because the watch event above not only triggers before a USB Drive is inserted/removed, ; but also AFTER insertion too, and if not reset, a subsequent CD insertion will trigger it again. ; So, every second, we reset $WATCH, to keep things clean Sleep (1000) $WATCH = False WEn Hope this keeps some hairs on your head!! Please keep in mind that the links, the application, nor the script are warrantied by Microsoft. Further, since the links are not hosted by MSFT they may change without notice. Network Systems Engineer * Zvetco Biometrics * Windows Server 2008 R2 * Core2 6600 @ 3.30GHz * 16 GIGS RAM * NVIDIA 9400GT * ** " Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
Free Windows Admin Tool Kit Click here and download it now
November 4th, 2010 2:39pm

DaveLivingstone wrote: > I am trying to create a scheduled task,so that when i connect my EXT > HDD the batchfile will automaticly run and backup my data to my Win7 > Pro PC Internal HDD. > > I have created the batchfile, and it will backup the ext hdd to the > int hdd on logon. > > But how can i get it to run the batch file ONLY when i connect the > ext HDD to the USB port. A VBScript can do the job. Here is one that will look for the missing USB drive until it is installed. Then it will run a batch file and exit. The script can be included in the Startup folder so that it runs when you log in. It uses minimal resources. You'll have to change the drive designation and batch file name to match your needs. '----------------------------DriveChk.vbs---------------------------------- set fso=CreateObject("Scripting.FileSystemObject") set WshShell=CreateObject("WScript.Shell") usbdrive="G" batchfile="batch.bat" do if drivechk(usbdrive)<>"" then exit do wscript.sleep 1000 loop WshShell.run "cmd /c " & batchfile function drivechk(d) on error resume next set chk=fso.getDrive(d) if err.number then err.clear:exit function drivechk=chk.path end function '-------------------------------------------------------------------------------- -- Crash "When you want to fool the world, tell the truth." ~ Otto von Bismarck ~
November 4th, 2010 4:13pm

First: I know nothing I downloaded the AutoIt and SciTE editor compiled the script and ran it. It starts (running as an icon in the tray) but the script pauses. I inserted the usb drive and viola nothing happened. Can you hold my hand through this?
Free Windows Admin Tool Kit Click here and download it now
March 2nd, 2011 7:31pm

Have you thought about setting up an Autorun.inf in the root of your USB HDD to run the batch file of your choice?
March 2nd, 2011 7:58pm

Hi Dave, This is script worked perfectly. I just have a question, the drivecheck.vbs wont run when I insert the usb drive, it will only run after i clicked the vbs file. Any suggestions how to do it? havent tried to code vbs scripting.. so, i really need your help on this.. :) thanks..
Free Windows Admin Tool Kit Click here and download it now
April 26th, 2011 8:10am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics